/**
 * Copyright (c) 2018-2099, Chill Zhuang 庄骞 (bladejava@qq.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.restaurant.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.restaurant.entity.OperatingIncome;
import org.springblade.modules.restaurant.service.IOperatingIncomeService;
import org.springblade.modules.restaurant.vo.OperatingIncomeVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 营业收入表 控制器
 *
 * @author Blade
 * @since 2025-02-05
 */
@RestController
@AllArgsConstructor
@RequestMapping("/restaurant/operatingincome")
@Tag(name = "营业收入表", description = "营业收入表接口")
public class OperatingIncomeController extends BladeController {

	private IOperatingIncomeService operatingIncomeService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入operatingIncome")
	public R<OperatingIncome> detail(OperatingIncome operatingIncome) {
		OperatingIncome detail = operatingIncomeService.getOne(Condition.getQueryWrapper(operatingIncome));
		return R.data(detail);
	}

	/**
	 * 分页 营业收入表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入operatingIncome")
	public R<IPage<OperatingIncome>> list(OperatingIncome operatingIncome, Query query) {
		IPage<OperatingIncome> pages = operatingIncomeService.page(Condition.getPage(query), Condition.getQueryWrapper(operatingIncome).orderByDesc("operating_time"));
		return R.data(pages);
	}

	/**
	 * 自定义分页 营业收入表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description = "传入operatingIncome")
	public R<IPage<OperatingIncomeVO>> page(OperatingIncomeVO operatingIncome, Query query) {
		IPage<OperatingIncomeVO> pages = operatingIncomeService.selectOperatingIncomePage(Condition.getPage(query), operatingIncome);
		return R.data(pages);
	}

	/**
	 * 新增 营业收入表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description = "传入operatingIncome")
	public R save(@Valid @RequestBody OperatingIncome operatingIncome) {
		// 判断当天是否已经有数据
		OperatingIncome one = operatingIncomeService.getOne(Wrappers.<OperatingIncome>lambdaQuery().eq(OperatingIncome::getOperatingTime, operatingIncome.getOperatingTime()));
		if (one != null) {
			return R.fail("当天已经有数据了");
		}
		// 计算合计 为空则设置为0
		operatingIncome.setTotalIncome(operatingIncome.getDineInIncome().add(operatingIncome.getTakeoutIncome()).add(operatingIncome.getProductIncome()).add(operatingIncome.getOtherIncome()));
		// 计算营业毛利
		operatingIncome.setProfitAmount(operatingIncome.getTotalIncome().subtract(operatingIncome.getProcurementExpenses()));
		// 计算总支出
		operatingIncome.setExpendAmount(operatingIncome.getProcurementExpenses().add(operatingIncome.getFixedExpenses()).add(operatingIncome.getSalaryExpenses()).add(operatingIncome.getOtherExpenses()));

		return R.status(operatingIncomeService.save(operatingIncome));
	}

	/**
	 * 修改 营业收入表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description = "传入operatingIncome")
	public R update(@Valid @RequestBody OperatingIncome operatingIncome) {
		// 计算合计 为空则设置为0
		operatingIncome.setTotalIncome(operatingIncome.getDineInIncome().add(operatingIncome.getTakeoutIncome()).add(operatingIncome.getProductIncome()).add(operatingIncome.getOtherIncome()));
		// 计算营业毛利
		operatingIncome.setProfitAmount(operatingIncome.getTotalIncome().subtract(operatingIncome.getProcurementExpenses()));
		// 计算总支出
		operatingIncome.setExpendAmount(operatingIncome.getProcurementExpenses().add(operatingIncome.getFixedExpenses()).add(operatingIncome.getSalaryExpenses()).add(operatingIncome.getOtherExpenses()));

		return R.status(operatingIncomeService.updateById(operatingIncome));
	}

	/**
	 * 新增或修改 营业收入表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description = "传入operatingIncome")
	public R submit(@Valid @RequestBody OperatingIncome operatingIncome) {
		// 计算合计 为空则设置为0
		operatingIncome.setTotalIncome(operatingIncome.getDineInIncome().add(operatingIncome.getTakeoutIncome()).add(operatingIncome.getProductIncome()).add(operatingIncome.getOtherIncome()));
		// 计算营业毛利
		operatingIncome.setProfitAmount(operatingIncome.getTotalIncome().subtract(operatingIncome.getProcurementExpenses()));
		// 计算总支出
		operatingIncome.setExpendAmount(operatingIncome.getProcurementExpenses().add(operatingIncome.getFixedExpenses()).add(operatingIncome.getSalaryExpenses()).add(operatingIncome.getOtherExpenses()));

		return R.status(operatingIncomeService.saveOrUpdate(operatingIncome));
	}


	/**
	 * 删除 营业收入表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "逻辑删除", description = "传入ids")
	public R remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(operatingIncomeService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 重新计算
	 */
	@PostMapping("/calculate")
	@ApiOperationSupport(order = 8)
	@Operation(summary = "重新计算", description = "传入ids")
	public R calculate(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		List<OperatingIncome> list = operatingIncomeService.listByIds(Func.toLongList(ids));
		for (OperatingIncome operatingIncome : list) {
			String operatingTime = operatingIncome.getOperatingTime();
			String tenantId = operatingIncome.getTenantId();
			operatingIncomeService.calculate(operatingIncome, operatingTime, tenantId);
		}
		return R.success("计算成功");
	}

}
